<?php
namespace app\admin\controller;

use app\admin\AdminCommon;
use think\Db;

class Comments extends AdminCommon
{
    //查询评论
    public function getindex()
    {
      $data=Db::table('tp_goods')->alias('tg')->field('tg.goods_name,tg.goods_pic,tc.id,tc.contents,tc.create_time,tu.user_name,tu.user_pic')->join('tp_comments tc','tg.id=tc.goods_id','right')->join('tp_user tu','tc.user_id=tu.id','left')->select();
        $i=0;
        foreach($data as $v)
        {
            $data[$i]['goods_pic']='/uploads/goods_pic/'.$v['goods_pic'];
            $data[$i]['user_pic']='/uploads/user_pic/'.$v['user_pic'];
            $data[$i]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            $i++;
        }
      return $this->fetch('comments/index',['data'=>$data,'b'=>1]);
    }

    //查看评论详情
    public function getdetail()
    {
        $request=$this->request;
        $res=$request->param();
        $data=Db::table('tp_comments')->where('id',$res['id'])->find();
        return $this->fetch('comments/detail',['data'=>$data]);
    }

    //加载回复模板
    public function getreply()
    {
        $request=$this->request;
        $res=$request->param();

        $result=Db::table('tp_replys')
            ->where('comments_id',$res['id'])
            ->select();
        if ($result ==null){
            $data=Db::table('tp_comments')->where('id',$res['id'])->find();
            $gid=$data['goods_id'];
            $datas=Db::table('tp_goods')->where('id',$gid)->find();
            $datas['goods_pic']='/uploads/goods_pic/'.$datas['goods_pic'];
            $this->assign('data',$data);
            $this->assign('datas',$datas);
            return $this->fetch('comments/replys');
        }else {
                //判断插入是否成功
                $data=[
                    'status'=>1,
                    'msg'=>'你已经添加过回复，不能再添加回复',
                ];
            return $data;

        }

    }

    //处理回复
    public function postdoreply()
    {
        $request=$this->request;
        $res=$request->param();
        $res['create_time']=time();
        unset($res['action']);
        $result=Db::table('tp_replys')->insert($res);
        if($result) {
            //判断插入是否成功
            $data=[
                'status'=>1,
                'msg'=>'回复完成',
            ];
        }else {
            $data=[
                'status'=>0,
                'msg' => '回复失败',
            ];
        };
        return $data;
    }

    //查询回复
    public  function getre_index()
     {
         $data=Db::table('tp_replys')->alias('trs')->field('trs.id trid,trs.comments_contents,trs.create_time,tc.id,tc.contents')->join('tp_comments tc','tc.id=trs.comments_id')->select();
         $i=0;
         foreach($data as $v)
         {
             $data[$i]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
             $i++;
         }
         return $this->fetch('comments/re_index',['data'=>$data,'b'=>1]);
     }

     //查看回复详情
     public function getreplys()
     {
         $request=$this->request;
         $res=$request->param();
         $data=Db::table('tp_replys')->where('id',$res['id'])->find();
         return $this->fetch('comments/r_detail',['data'=>$data]);
     }

     //删除回复
    public function postdelete()
    {
        $request=$this->request;
        $res=$request->param();
        $result=Db::table('tp_replys')->where('id',$res['id'])->delete();
        if($result){
            $data=[
                'status'=>1,
                'msg'=>'删除成功',
            ];
        }else{
            $data=[
                'status'=>0,
                'msg'=>'删除失败',
            ];
        }
        return $data;
    }

    //批量删除
    public function postdatadel(){
        $request=$this->request;
        $res=$request->param();
        $ids=explode(',',$res['ids']);
        Db::startTrans();
        try{
            foreach($ids as $id)
            {
                $result=Db::table('tp_replys')->where('id',$id)->delete();
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
        if($result){
            $data=[
                'status'=>0,
                'msg'=>'批量删除成功',
            ];
        }else{
            $data=[
                'status'=>1,
                'msg'=>'批量删除失败',
            ];
        }
        return $data;
    }
}
?>